package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_ACTIVITY_GET_REWARD, desc = "获取活动奖励")
public class GetActivityRewardCmd extends AbstractCommand {

	private int activityId;

	private int conditionId;

	private ActivityRewardResult rewardResult;

	private boolean isNeedResp = true;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ActivityProto.ActivityGetRewardReqMsg reqMsg = ActivityProto.ActivityGetRewardReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		conditionId = reqMsg.getConditionId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
		if(null == activityInfo){
			rewardResult = new ActivityRewardResult();
			rewardResult.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
			return;
		}
		// 冲榜活动
		if (activityInfo.getType() == eActivityType.RankActivity.getValue()) {
			rewardResult = player.getModule(ActivityModule.class).getRankActivityReward(activityId);
        } else if (activityInfo.getType() == eActivityType.UnionRankActivity.getValue()) {
            rewardResult = player.getModule(ActivityModule.class).getRankActivityReward(activityId);

        } else if (activityInfo.getType() == eActivityType.UnionActivity.getValue()) {
            rewardResult = player.getModule(ActivityModule.class).getUnionActivityReward(activityId, conditionId);
            if (rewardResult.getRet() == 0) {
                isNeedResp = false;
            }
        } else if (activityInfo.getType() == eActivityType.CrossRankActivity.getValue() ||
                activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue() ||
                activityInfo.getType() == eActivityType.CrossDunHuangShiKuActivity.getValue() ||
                activityInfo.getType() == eActivityType.MusicRoomRank.getValue()
		) {
            rewardResult = player.getModule(ActivityModule.class).getCrossRankActivityReward(activityId, conditionId, false, 0);
            if (rewardResult.getRet() == 0) {
                isNeedResp = false;
            }
        } else {
			rewardResult = player.getModule(ActivityModule.class).getActivityReward(activityId, conditionId);
        }
	}

	@Override
	public void sendData(GamePlayer player) {
		if(isNeedResp) {
			rewardResult.setActivityId(activityId);
			rewardResult.setConditionId(conditionId);
			ActivityProto.ActivityGetRewardRespMsg.Builder respMsg = ActivityPb.parseActivityGetRewardRespMsg(rewardResult);
			if(null != respMsg) {
				player.sendPacket(Protocol.U_ACTIVITY_GET_REWARD, respMsg);
			}
		}
	}

}
